{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Node With Highest Edge Score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #graph #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #图 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: edgeScore"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #边积分最高的节点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个有向图，图中有 <code>n</code> 个节点，节点编号从 <code>0</code> 到 <code>n - 1</code> ，其中每个节点都 <strong>恰有一条</strong> 出边。</p>\n",
    "\n",
    "<p>图由一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的整数数组 <code>edges</code> 表示，其中 <code>edges[i]</code> 表示存在一条从节点 <code>i</code> 到节点 <code>edges[i]</code> 的 <strong>有向</strong> 边。</p>\n",
    "\n",
    "<p>节点 <code>i</code> 的 <strong>边积分</strong> 定义为：所有存在一条指向节点 <code>i</code> 的边的节点的 <strong>编号</strong> 总和。</p>\n",
    "\n",
    "<p>返回 <strong>边积分</strong> 最高的节点。如果多个节点的 <strong>边积分</strong> 相同，返回编号 <strong>最小</strong> 的那个。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img src=\"https://assets.leetcode.com/uploads/2022/06/20/image-20220620195403-1.png\" style=\"width: 450px; height: 260px;\">\n",
    "<pre><strong>输入：</strong>edges = [1,0,0,0,0,7,7,5]\n",
    "<strong>输出：</strong>7\n",
    "<strong>解释：</strong>\n",
    "- 节点 1、2、3 和 4 都有指向节点 0 的边，节点 0 的边积分等于 1 + 2 + 3 + 4 = 10 。\n",
    "- 节点 0 有一条指向节点 1 的边，节点 1 的边积分等于 0 。\n",
    "- 节点 7 有一条指向节点 5 的边，节点 5 的边积分等于 7 。\n",
    "- 节点 5 和 6 都有指向节点 7 的边，节点 7 的边积分等于 5 + 6 = 11 。\n",
    "节点 7 的边积分最高，所以返回 7 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img src=\"https://assets.leetcode.com/uploads/2022/06/20/image-20220620200212-3.png\" style=\"width: 150px; height: 155px;\">\n",
    "<pre><strong>输入：</strong>edges = [2,0,0,2]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：\n",
    "</strong>- 节点 1 和 2 都有指向节点 0 的边，节点 0 的边积分等于 1 + 2 = 3 。\n",
    "- 节点 0 和 3 都有指向节点 2 的边，节点 2 的边积分等于 0 + 3 = 3 。\n",
    "节点 0 和 2 的边积分都是 3 。由于节点 0 的编号更小，返回 0 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == edges.length</code></li>\n",
    "\t<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= edges[i] &lt; n</code></li>\n",
    "\t<li><code>edges[i] != i</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [node-with-highest-edge-score](https://leetcode.cn/problems/node-with-highest-edge-score/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [node-with-highest-edge-score](https://leetcode.cn/problems/node-with-highest-edge-score/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,0,0,0,0,7,7,5]', '[2,0,0,2]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n=len(edges)\n",
    "        g=[0]*n\n",
    "        a=0\n",
    "        for i,v in enumerate(edges):\n",
    "            g[v]+=i\n",
    "            if g[v]>g[a] or g[v]==g[a] and v<a:\n",
    "                a=v\n",
    "        # for i,v in enumerate(g):\n",
    "        #     if v>g[a]:\n",
    "        #         a=i\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n=len(edges)\n",
    "        g=[0]*n\n",
    "        a=0\n",
    "        for i,v in enumerate(edges):\n",
    "            g[v]+=i\n",
    "            if g[v]>g[a] or g[v]==g[a] and v<a:\n",
    "                a=v\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        res = [0]*len(edges)\n",
    "        for i in range(len(edges)):\n",
    "            res[edges[i]] += i\n",
    "        return res.index(max(res))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "                ans = to\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n=len(edges)\n",
    "        g=[0]*n\n",
    "        a=0\n",
    "        for i,v in enumerate(edges):\n",
    "            g[v]+=i\n",
    "            if g[v]>g[a] or g[v]==g[a] and v<a:\n",
    "                a=v\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "        for i,to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "                ans = to\n",
    "        return ans\n",
    "        # n = len(edges)\n",
    "        # score = [0] * n\n",
    "\n",
    "        # for i in range(n):\n",
    "        #     score[edges[i]] += i\n",
    "        \n",
    "        # ans = 0\n",
    "        # mx = 0\n",
    "        # for i in range(n):\n",
    "        #     if score[i] > mx:\n",
    "        #         mx = score[i]\n",
    "        #         ans = i\n",
    "        # return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "                ans = to\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ind = [0] * len(edges)\n",
    "        for i, x in enumerate(edges):\n",
    "            ind[x] += i\n",
    "        maxval = max(ind)\n",
    "        for i, x in enumerate(ind):\n",
    "            if x == maxval:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        g = [0] * n\n",
    "        for idx, i in enumerate(edges):\n",
    "            g[i] += idx\n",
    "\n",
    "        return g.index(max(g))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "                ans = to\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        point = [0]*len(edges)\n",
    "        for i,n in enumerate(edges):\n",
    "            point[n]+=i\n",
    "        return point.index(max(point))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        la=[0]*len(edges)\n",
    "        for i in range(len(edges)):la[edges[i]]+=i\n",
    "        return la.index(max(la))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        scores = [0] * len(edges)\n",
    "        for i, e in enumerate(edges):\n",
    "            scores[e] += i\n",
    "        max_score = max(scores)\n",
    "        for i, s in enumerate(scores):\n",
    "            if s == max_score:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        cnt = [0] * n \n",
    "        for i, j in enumerate(edges):\n",
    "            cnt[j] += i \n",
    "        ans = -1\n",
    "        tmp = -1\n",
    "        for i, j in enumerate(cnt):\n",
    "            if j > tmp:\n",
    "                ans = i \n",
    "                tmp = j \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "                ans = to\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "                ans = to\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "      n = len(edges)\n",
    "      score = [0]*n\n",
    "      for i,j in enumerate(edges):\n",
    "        score[j] += i\n",
    "      return max(range(n),key=lambda x:(score[x],-x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        cnt = [0] * n \n",
    "        for i, j in enumerate(edges):\n",
    "            cnt[j] += i \n",
    "        return cnt.index(max(cnt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "                ans = to\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n=len(edges)\n",
    "        g=[0]*n\n",
    "        a=0\n",
    "        for i,v in enumerate(edges):\n",
    "            g[v]+=i\n",
    "        for i,v in enumerate(g):\n",
    "            if v>g[a]:\n",
    "                a=i\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        score = [0] * n\n",
    "        for i, x in enumerate(edges):\n",
    "            score[x] += i\n",
    "        ans = mx = 0\n",
    "        for i, s in enumerate(score):\n",
    "            if s > mx:\n",
    "                mx = s\n",
    "                ans = i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        cnt = [0] * n\n",
    "        for i in range(n):\n",
    "            cnt[edges[i]] += i\n",
    "        m = max(cnt)\n",
    "        return next(i for i in range(n) if cnt[i] == m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n=len(edges)\n",
    "        g=[0]*n\n",
    "        a=0\n",
    "        for i,v in enumerate(edges):\n",
    "            g[v]+=i\n",
    "            if g[v]>g[a] or g[v]==g[a] and v<a:\n",
    "                a=v\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        d=dict()\n",
    "        la=[0]*len(edges)\n",
    "        for i in range(len(edges)):la[edges[i]]+=i\n",
    "        ind=0\n",
    "        val=-1\n",
    "        for i in range(len(edges)):\n",
    "            if la[i]>val:val,ind=la[i],i\n",
    "        return ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        N = len(edges)\n",
    "        scores = [0] * N\n",
    "        for i, target in enumerate(edges):\n",
    "            scores[target] += i\n",
    "        res = 0\n",
    "        mx = scores[0]\n",
    "        for i in range(1, N):\n",
    "            if scores[i] > mx:\n",
    "                mx = scores[i]\n",
    "                res = i\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n=len(edges)\n",
    "        d=[0]*n\n",
    "        for i,e in enumerate(edges):\n",
    "            d[e]+=i\n",
    "        ans=0\n",
    "        ret=0\n",
    "        for i in range(n):\n",
    "            if d[i]>ans:\n",
    "                ans=d[i]\n",
    "                ret=i\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n=len(edges)\n",
    "        deg=[0]*n\n",
    "        for i,x in enumerate(edges):\n",
    "            deg[x] += i\n",
    "        val=max(deg)\n",
    "        for i,x in enumerate(deg):\n",
    "            if x==val:\n",
    "                return i\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "\n",
    "        for i, to in enumerate(edges):\n",
    "\n",
    "            score[to] += i\n",
    "\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "\n",
    "                ans = to\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        ans = [0]*n\n",
    "        value = ans[0]\n",
    "        k = 0\n",
    "        for i,x in enumerate(edges):\n",
    "            ans[x]+=i\n",
    "        for i,x in enumerate(ans):\n",
    "            if value<x:\n",
    "                k = i\n",
    "                value = x\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        d=dict()\n",
    "        la=[0]*len(edges)\n",
    "        for i in range(len(edges)):la[edges[i]]+=i\n",
    "        ind=0\n",
    "        val=-1\n",
    "        return la.index(max(la))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        # n个点 n条边\n",
    "        n = len(edges)\n",
    "        score = [0]*n\n",
    "        for i in range(n):\n",
    "            score[edges[i]]+=i\n",
    "        max_p, idx = -1,-1\n",
    "        for i in range(n):\n",
    "            if score[i]>max_p:\n",
    "                max_p = score[i]\n",
    "                idx = i\n",
    "        return idx\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        score = [0]*n\n",
    "        for i in range(n):\n",
    "            score[edges[i]] += i\n",
    "        return score.index(max(score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans, score = 0, [0] * len(edges)\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[to] > score[ans] or score[to] == score[ans] and to < ans:\n",
    "                ans = to\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n=len(edges)\n",
    "        gg=[0]*n\n",
    "        ans=0\n",
    "        for i,v in enumerate(edges):\n",
    "            gg[v]+=i\n",
    "        for i,v in enumerate(gg):\n",
    "            if v>gg[ans]:\n",
    "                ans=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        count = [0] * n\n",
    "        for u, v in enumerate(edges):\n",
    "            count[v] += u\n",
    "        return count.index(max(count))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        d=dict()\n",
    "        la=[0]*len(edges)\n",
    "        for i in range(len(edges)):la[edges[i]]+=i\n",
    "        ind=0\n",
    "        val=-1\n",
    "        return la.index(max(la))\n",
    "        # for i in range(len(edges)):\n",
    "        #     if la[i]>val:val,ind=la[i],i\n",
    "        # return ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        result = [0] * len(edges)\n",
    "        for i in range(len(edges)):\n",
    "            result[edges[i]] += i\n",
    "        \n",
    "        max_val = result[-1]\n",
    "        max_idx = len(result) - 1\n",
    "\n",
    "        for idx in range(len(result) - 2, -1, -1):\n",
    "            if result[idx] >= max_val:\n",
    "                max_val = result[idx]\n",
    "                max_idx = idx\n",
    "        \n",
    "        return max_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        node = [0]*len(edges)\n",
    "        for i,x in enumerate(edges):\n",
    "            node[x]+=i\n",
    "        # print(node)\n",
    "        ans = 0\n",
    "        for i,x in enumerate(node):\n",
    "            if x>node[ans]:\n",
    "                ans = i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        score = [0]*n\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "        ans = 0\n",
    "        for i, s in enumerate(score):\n",
    "            if score[ans] < s:\n",
    "                ans = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        score = [0]*n\n",
    "        ans = 0\n",
    "        for i, to in enumerate(edges):\n",
    "            score[to] += i\n",
    "            if score[ans] < score[to] or score[ans] == score[to] and to < ans:\n",
    "                ans = to\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        l = [0] * len(edges)\n",
    "        for i, n in enumerate(edges):\n",
    "            l[n] += i\n",
    "        ans = mx = 0\n",
    "        for i, n in enumerate(l):\n",
    "            if n > mx:\n",
    "                mx = n\n",
    "                ans = i\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        # 初始化边积分列表\n",
    "        scores = [0] * len(edges)\n",
    "        \n",
    "        # 遍历每个边并增加对应节点的边积分\n",
    "        for i, target in enumerate(edges):\n",
    "            scores[target] += i\n",
    "        \n",
    "        # 返回最高边积分的节点编号\n",
    "        # max函数中的key参数用于在分数相同的情况下返回编号最小的节点\n",
    "        return max(range(len(scores)), key=lambda i: (scores[i], -i))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        scores = [0] * n\n",
    "\n",
    "        for s, i in enumerate(edges):\n",
    "            scores[i] += s\n",
    "\n",
    "        max_score = 0\n",
    "        max_score_node = 0\n",
    "        for i, s in enumerate(scores):\n",
    "            if (s > max_score) or (s == max_score and i <= max_score_node):\n",
    "                max_score = s\n",
    "                max_score_node = i\n",
    "\n",
    "        return max_score_node\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        d=dict()\n",
    "        la=[0]*len(edges)\n",
    "        for i in range(len(edges)):la[edges[i]]+=i\n",
    "        ind=0\n",
    "        val=-1\n",
    "        return la.index(max(la))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        scores = [0] * (max(edges) + 1)\n",
    "        \n",
    "        for i in range(len(edges)):\n",
    "            scores[edges[i]] += i\n",
    "            \n",
    "        index = -1\n",
    "        ans = 0\n",
    "        for i in range(len(scores)):\n",
    "            if scores[i] > ans:\n",
    "                ans = scores[i]\n",
    "                index = i\n",
    "                \n",
    "        return index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "      n = len(edges)\n",
    "      scores = [0] * n\n",
    "\n",
    "      for i in range(n):\n",
    "        scores[edges[i]] += i\n",
    "\n",
    "      res = 0\n",
    "      ma = scores[0]\n",
    "      for i in range(1, n):\n",
    "        if scores[i] > ma:\n",
    "          res = i\n",
    "          ma = scores[i]\n",
    "\n",
    "      return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        scores = [0]*n\n",
    "        for i, j in enumerate(edges):\n",
    "            scores[j] += i\n",
    "        _max = result = 0\n",
    "        for i, s in enumerate(scores):\n",
    "            if s > _max:\n",
    "                result = i\n",
    "                _max = s\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        score = [0] * n\n",
    "        for i, x in enumerate(edges):\n",
    "            score[x] += i\n",
    "        ans = mx = 0\n",
    "        for i, s in enumerate(score):\n",
    "            if s > mx:\n",
    "                mx = s\n",
    "                ans = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "\n",
    "        n = len(edges)\n",
    "        a = [0] * n\n",
    "        score = 0\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            a[edges[i]] += i\n",
    "            if a[edges[i]] > score:\n",
    "                score = a[edges[i]]\n",
    "                ans = edges[i]\n",
    "            elif a[edges[i]] == score:\n",
    "                ans = min(ans, edges[i])\n",
    "        return ans\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        ans = [0 for _ in range(n)]\n",
    "        for i in range(n):\n",
    "            ans[edges[i]] += i \n",
    "        maxx = 0\n",
    "        for j in range(n):\n",
    "            if ans[j] > ans[maxx]:\n",
    "                maxx = j\n",
    "        return maxx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        ans = [0 for _ in range(len(edges))]\n",
    "\n",
    "        for (i,v) in enumerate(edges):\n",
    "            ans[v] += i\n",
    "\n",
    "        res = -1\n",
    "        tmp = -1\n",
    "        for (i,v) in enumerate(ans):\n",
    "            if v > tmp:\n",
    "                tmp = v\n",
    "                res = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        d=dict()\n",
    "        la=[0]*len(edges)\n",
    "        for i in range(len(edges)):la[edges[i]]+=i\n",
    "        ind=0\n",
    "        val=-1\n",
    "        print(d)\n",
    "        for i in range(len(edges)):\n",
    "            print(val,ind)\n",
    "            if la[i]>val:val,ind=la[i],i\n",
    "        return ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        max_value = float(\"-inf\")\n",
    "        max_index = 0\n",
    "        record = {}\n",
    "        for index, ele in enumerate(edges):\n",
    "            if ele not in record:\n",
    "                record[ele] = index\n",
    "            else:\n",
    "                record[ele] += index\n",
    "            if record[ele] > max_value:\n",
    "                max_index = ele\n",
    "                max_value = record[ele]\n",
    "            elif record[ele] == max_value:\n",
    "                if ele < max_index:\n",
    "                    max_index = ele\n",
    "        return max_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        dic = defaultdict(int) #dic[i]表示 节点i 的边积分\n",
    "        n = len(edges)\n",
    "        maximum = 0\n",
    "        ans = 1000000\n",
    "        for i in range(n):\n",
    "            dic[edges[i]] += i\n",
    "            if(dic[edges[i]]>=maximum):\n",
    "                ans = edges[i] if dic[edges[i]] > maximum else min(ans,edges[i])\n",
    "                maximum = dic[edges[i]]\n",
    "            #print(ans)\n",
    "        #print(dic)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        dic = {}\n",
    "        for i, x  in enumerate(edges):\n",
    "            dic[x] = dic.get(x, 0) + i\n",
    "        max_sum = 0\n",
    "        min_index = -1\n",
    "        for k,v in dic.items():\n",
    "            if v>max_sum:\n",
    "                max_sum = v\n",
    "                min_index = k\n",
    "            elif v == max_sum:\n",
    "                min_index = min(k, min_index)\n",
    "        return min_index "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        dict = {}\n",
    "        length = len(edges)\n",
    "        for i in range(length):\n",
    "            if edges[i] not in dict:\n",
    "                dict[edges[i]] = i\n",
    "            else:\n",
    "                dict[edges[i]] += i\n",
    "        a = max(dict.values())\n",
    "        for i in range(length):\n",
    "            if i in dict and dict[i] == a:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        length = len(edges)\n",
    "        dict = {}\n",
    "        for i in range(length):\n",
    "            a = edges[i]\n",
    "            if a not in dict:\n",
    "                dict[a] = i\n",
    "            else:\n",
    "                dict[a] += i\n",
    "        e = max(dict.values())\n",
    "        for i in range(length):\n",
    "            if i in dict and dict[i] == e:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        max_value = float(\"-inf\")\n",
    "        max_index = 0\n",
    "        record = {}\n",
    "        for index, ele in enumerate(edges):\n",
    "            if ele not in record:\n",
    "                record[ele] = index\n",
    "                if record[ele] > max_value:\n",
    "                    max_index = ele\n",
    "                    max_value = record[ele]\n",
    "                elif record[ele] == max_value:\n",
    "                    if ele < max_index:\n",
    "                        max_index = ele\n",
    "            else:\n",
    "                record[ele] += index\n",
    "                if record[ele] > max_value:\n",
    "                    max_index = ele\n",
    "                    max_value = record[ele]\n",
    "                elif record[ele] == max_value:\n",
    "                    if ele < max_index:\n",
    "                        max_index = ele\n",
    "        return max_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        max_value = -1\n",
    "        max_index = 0\n",
    "        record = {}\n",
    "        for index, ele in enumerate(edges):\n",
    "            if ele not in record:\n",
    "                record[ele] = index\n",
    "            else:\n",
    "                record[ele] += index\n",
    "            if record[ele] > max_value:\n",
    "                max_index = ele\n",
    "                max_value = record[ele]\n",
    "            elif record[ele] == max_value:\n",
    "                if ele < max_index:\n",
    "                    max_index = ele\n",
    "        return max_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        max_value = float(\"-inf\")\n",
    "        max_index = 0\n",
    "        record = {}\n",
    "        for index, ele in enumerate(edges):\n",
    "            if ele not in record:\n",
    "                record[ele] = index\n",
    "            else:\n",
    "                record[ele] += index\n",
    "            if record[ele] > max_value:\n",
    "                max_index = ele\n",
    "                max_value = record[ele]\n",
    "            elif record[ele] == max_value:\n",
    "                if ele < max_index:\n",
    "                    max_index = ele\n",
    "        return max_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        cnt = defaultdict(int)\n",
    "        for k,v in enumerate(edges):\n",
    "            cnt[v] += k\n",
    "        maxv,ans = -inf,0\n",
    "        for k,v in cnt.items():\n",
    "            if v > maxv:\n",
    "                ans = k\n",
    "                maxv = v\n",
    "            elif v == maxv and k < ans:\n",
    "                ans = k\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        mydct={}\n",
    "        haoresult=0\n",
    "        result=[]\n",
    "        amax=-1\n",
    "        for i in range(len(edges)):\n",
    "            if edges[i] not in mydct:\n",
    "                mydct[edges[i]]=i\n",
    "            else:\n",
    "                mydct[edges[i]]+=i\n",
    "        print(mydct)\n",
    "        for i,j in mydct.items():\n",
    "            if j>amax:\n",
    "                amax=j\n",
    "                haoresult=i\n",
    "        for i,j in mydct.items():\n",
    "            if j==amax:\n",
    "                result.append(i)\n",
    "        return min(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        dic = {}\n",
    "        for i in range(len(edges)):\n",
    "            if edges[i] in dic:\n",
    "                dic[edges[i]] += i\n",
    "            else:\n",
    "                dic[edges[i]] = i\n",
    "        s = 0\n",
    "        index = 0\n",
    "        print(dic)\n",
    "        for n, i in enumerate(list(dic.keys())):\n",
    "            print(dic[i])\n",
    "            if dic[i] > s:\n",
    "                index = i\n",
    "                s = dic[i]\n",
    "            if dic[i] == s:\n",
    "                index = min(index, i)\n",
    "        return index\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        edge_table = dict()\n",
    "        edge_sum_max = 0\n",
    "        edge_node = 2**31 - 1\n",
    "        for i in range(len(edges)):\n",
    "            edge_table[edges[i]] = edge_table.get(edges[i], '') + str(i) + '|'\n",
    "        for edge_item in edge_table.items():\n",
    "            edge_sum = 0\n",
    "            edges = edge_item[1].split('|')\n",
    "            for edge in edges:\n",
    "                if len(edge) > 0:\n",
    "                    edge_sum += int(edge)\n",
    "            if edge_sum > edge_sum_max:\n",
    "                edge_sum_max = edge_sum\n",
    "                edge_node = edge_item[0]\n",
    "            elif  edge_sum == edge_sum_max:\n",
    "                if edge_item[0] < edge_node:\n",
    "                    edge_node = edge_item[0]\n",
    "            else:\n",
    "                continue\n",
    "        return edge_node\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        from collections import defaultdict\n",
    "        dic = defaultdict(int)\n",
    "        for i, edge in enumerate(edges):\n",
    "            dic[edge] += i\n",
    "        l = []\n",
    "        for k, v in dic.items():\n",
    "            l.append([k, v])\n",
    "        l.sort(key=lambda x:x[0])\n",
    "        ans = 0\n",
    "        for v in l:\n",
    "            if v[1] > ans:\n",
    "                ans = v[1]\n",
    "                index = v[0]\n",
    "        return index "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        from collections import defaultdict\n",
    "        dic = defaultdict(int)\n",
    "        for i, edge in enumerate(edges):\n",
    "            dic[edge] += i\n",
    "        l = []\n",
    "        for k, v in dic.items():\n",
    "            l.append([k, v])\n",
    "        l.sort(key=lambda x:x[0])\n",
    "        ans = 0\n",
    "        for v in l:\n",
    "            if v[1] > ans:\n",
    "                ans = v[1]\n",
    "                index = v[0]\n",
    "        return index "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        n = len(edges)\n",
    "        mp = {}\n",
    "        for i in range(n):\n",
    "            u = mp.get(edges[i], [])\n",
    "            u.append(i)\n",
    "            mp[edges[i]] = u\n",
    "        ans, cnt = float('inf'), 0\n",
    "        for k, v in mp.items():\n",
    "            s = sum(v)\n",
    "            if s == cnt:\n",
    "                ans = min(ans, k)\n",
    "            elif s > cnt:\n",
    "                cnt = s\n",
    "                ans = k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, ed: List[int]) -> int:\n",
    "        n=len(ed)\n",
    "        #print(n)\n",
    "        dic={i:0 for i in range(n)}\n",
    "        #print(dic)\n",
    "        for i,v in enumerate(ed):\n",
    "            dic[v]+=i\n",
    "        #print(dic)\n",
    "        t=dic[0]\n",
    "        res=0\n",
    "        for i,v in dic.items():\n",
    "            if v>t:\n",
    "                t=v\n",
    "                res=i\n",
    "            elif v==t:\n",
    "                res=min(i,res)\n",
    "            else:\n",
    "                pass\n",
    "        return res\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        dic = defaultdict(int)\n",
    "        for i,e in enumerate(edges):\n",
    "            dic[e] += i\n",
    "        return sorted(list(dic.items()),key=lambda x:(-x[1],x[0]))[0][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        out=defaultdict(int)\n",
    "        for i,edge in enumerate(edges):\n",
    "            out[edge]+=i\n",
    "        out=sorted(out.items(),key=lambda x:(-x[1],x[0]))\n",
    "        return out[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        dic = defaultdict(int)\n",
    "        for i,e in enumerate(edges):\n",
    "            dic[e] += i\n",
    "        # print(dic)\n",
    "        a = list(dic.items())\n",
    "        # print(a)\n",
    "        a.sort(key=lambda x:(-x[1],x[0]))\n",
    "        # print(a)\n",
    "        return a[0][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        hashScore = {}\n",
    "        for i,e in enumerate(edges):\n",
    "            hashScore[e] = hashScore.get(e,0)+i\n",
    "        print(hashScore)\n",
    "        res = sorted(hashScore.items(),key=lambda x:(-x[1],x[0]))\n",
    "        return res[0][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def edgeScore(self, edges: List[int]) -> int:\n",
    "        total = defaultdict(int)\n",
    "        \n",
    "        for u, v in enumerate(edges):\n",
    "            total[v] += u\n",
    "        \n",
    "        record = []\n",
    "        for key in total:\n",
    "            record.append([key, total[key]])\n",
    "        \n",
    "        record.sort(key=lambda x: [-x[1], x[0]])\n",
    "        \n",
    "        return record[0][0]\n",
    "        \n",
    "            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
